/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.openmrs.module.basicmodule.extension.html;

import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.util.Map;
import org.openmrs.module.Extension;

public class PatientSummaryExtension extends Extension {

    	private String patientId = "";

    public Extension.MEDIA_TYPE getMediaType() {
        return Extension.MEDIA_TYPE.html;
    }
    
    public String getAlert(int patientID)
    {
        String output=new String();
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        PrintStream ps = new PrintStream(baos);
        // IMPORTANT: Save the old System.out!
        PrintStream old = System.out;
        // Tell Java to use your special stream
        System.setOut(ps);  
        // Print some output: goes to your special stream
        org.openmrs.module.basicmodule.DSS.compiler.Compiler compiler = (new org.openmrs.module.basicmodule.DSS.compiler.Compiler("activated.dss",patientID));
        // Put things back
        System.out.flush();
        
        output+=compiler.compileProgram();
        output+="\n"+baos.toString();
        System.setOut(old);                    
        System.out.println("Here getting alert for Patient Dashboard.");
        
        
        String fullAlert=new String();        
        int end_2=0;
        while (output.indexOf("#PatientDashboard#",end_2)!=-1)
        {
            int start_1=output.indexOf("#PatientDashboard#",end_2);        
            if ( start_1!=-1)
            {
                int start_2=output.indexOf("$", start_1);        
                System.out.println(output.substring(start_1, start_2));
                end_2=output.indexOf("$", start_2+1);
                System.out.println(output.substring(start_1, end_2));
                String alert=output.substring(start_2,end_2);
                System.out.println(alert);
                alert=alert.replace("$", "");       
                alert=alert.toUpperCase();
                fullAlert+= "<p><font color=\"red\" size=\"3\">ALERT : "+alert+"</font></p>";                   
            }
        }
        return fullAlert;
    }
    	@Override
	public String getOverrideContent(final String bodyContent) {
            //return " &nbsp;<a href='module/basicmodule/patientsummarylink.form?patientId=" + patientId + "' target='_blank'>View Summary</a> ";
            String alert=getAlert(Integer.parseInt(patientId));
            return "<b>OUTPUT ALERTS HERE FOR PATIENT ID: &nbsp;" + patientId + 
                    " <i>&nbsp;&nbsp;      CHECK IN URL FOR PATIENT NUMBER</i>&nbsp;&nbsp;&nbsp;&nbsp; "
                    +"<div style=\"float: right\"><a href=\"../openmrs/module/basicmodule/patientSummary.form?patientId="+patientId+"\" >View Patient Summary</a></div></b>"
                    +alert;
	}
        
    	@Override
	public void initialize(final Map<String, String> parameters) {
		patientId = parameters.get("patientId");
	}

}